﻿package src {

	import flash.display.*;
	import flash.net.*;
	import flash.events.*;
	import flash.media.*;

	public class Kfp extends MovieClip {

		private var url_mp3:String;
		private var url_kmc:String;
		
		private var sound:Sound;
		private var channel:SoundChannel;
		private var position:int = 0;
		private var duration:int = 0;
		private var paused:Boolean = false;
		private var stf:SoundTransform;

		public function Kfp() {
			stage.scaleMode = "noScale";
			//
			seekbar.bg.buttonMode = true;
			seekbar.bg.addEventListener(MouseEvent.CLICK, seekHandler);
			seekbar.track.mouseEnabled = false;
			seekbar.track.width = 0;
			
			
			stf = new SoundTransform();
			
			bt_play.addEventListener(MouseEvent.CLICK, playHandler);
			bt_pause.addEventListener(MouseEvent.CLICK, pauseHandler);
			bt_stop.addEventListener(MouseEvent.CLICK, stopHandler);

			var loader:URLLoader = new URLLoader();
			loader.addEventListener(Event.COMPLETE, loaded);
			loader.addEventListener(IOErrorEvent.IO_ERROR, errord);
			loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, errord);
			loader.load(new URLRequest("config.xml"));
		}

		private function loaded(e:Event):void {
			try {
				var xml:XMLList = new XMLList(e.target.data);
			} catch (e) {
				output(e);
			}
			url_mp3 = xml.@mp3;
			url_kmc = xml.@kmc;
		}
		private function errord(e:Event):void {
			output(e);
		}
		
		private function seekHandler(e:MouseEvent):void {
			var tw:Number = seekbar.bg.width;
			var tx:Number = seekbar.bg.mouseX;
			seekbar.track.width = tx;
			var per:Number = tx / tw;
			seek(per);
		}
		
		private function seek(per:Number):void {
			//没有长度不能搜索
			if (!sound || !sound.length) {
				return;
			}
			position = duration * per * 1000;
			start();
		}
		
		private function playHandler(e:MouseEvent):void {
			if (paused && sound) {
				start();
			} else {
				sound = new Sound();
				sound.load(new URLRequest(url_mp3));
				sound.addEventListener(Event.OPEN, openHandler);
				sound.addEventListener(IOErrorEvent.IO_ERROR, errord);
				sound.addEventListener(Event.COMPLETE, loadedHandler);
				sound.addEventListener(ProgressEvent.PROGRESS, progressHandler);
				output("connecting ...");
				kmc.load(url_kmc);
			}
		}
		private function pauseHandler(e:MouseEvent):void {
			if (!sound) {
				return;
			}
			if (paused) {
				start();
				paused = false;
				return;
			}
			removeEventListener(Event.ENTER_FRAME, timeHandler);
			position = channel.position;
			channelStop();
			paused = true;
			output("paused");
		}
		private function stopHandler(e:MouseEvent):void {
			removeEventListener(Event.ENTER_FRAME, timeHandler);
			paused = false;
			position = 0;
			duration = 0;
			output("stopped");
			seekbar.track.width = 0;
			showTime();
			channelStop();
			if (sound) {
				sound.removeEventListener(Event.OPEN, openHandler);
				sound.removeEventListener(Event.COMPLETE, loadedHandler);
				sound.removeEventListener(IOErrorEvent.IO_ERROR, errord);
				sound.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
				try {
					sound.close();
				} catch (e:Error) {
				}
				sound = null;
			}
			kmc.stop();
		}
		
		private function channelStop():void {
			if (channel) {
				channel.stop();
				channel.removeEventListener(Event.SOUND_COMPLETE, completeHandler);
			}
		}
		
		private function completeHandler(e:Event):void {
			paused = false;
			position = 0;
		}
		private function openHandler(e:Event):void {
			position = 0;
			start();
		}
		private function start():void {
			output("playing");
			if (position > sound.length) {
				position = sound.length;
			}
			channelStop();
			channel = sound.play(position);
			channel.addEventListener(Event.SOUND_COMPLETE, completeHandler);
			stf.volume = 1;
			channel.soundTransform = stf;
			addEventListener(Event.ENTER_FRAME, timeHandler);
		}
		
		private function progressHandler(e:ProgressEvent):void {
			var bt:Number = sound.bytesTotal;
			var bl:Number = sound.bytesLoaded;
			//下载进度
			var pre:Number = 0;
			if (bt > 0) {
				pre = bl / bt;
			}
			var per:String = "loading ... " + Math.round(pre * 100) + "%";
			output(per);
			if (pre > 0 && sound.length) {
				var len:Number = Math.floor(sound.length * 0.001 / pre);
				if (len > duration) {
					duration = len;
				}
			}
		}
		
		private function loadedHandler(e:Event):void {
			if (sound.length) {
				duration = sound.length * 0.001;
				output("loaded");
			}
		}
		
		private function timeHandler(e:Event):void {
			position = channel.position;
			kmc.update(position);
			//播放位置 单位秒
			showTime();
			//
			var tw:Number = seekbar.bg.width;
			var tx:Number = Math.round(tw * position * 0.001 / duration);
			seekbar.track.width = tx;
		}
		
		private function showTime():void {
			var seconds:Number = position * 0.001;
			time.text = sTime(seconds, duration);
		}
		private function sTime(p:Number, d:Number):String {
			return zero(p/60) + ":" + zero(p%60) + " / " + zero(d/60) + ":" + zero(d%60);
		}
		//补0
		private function zero(n:Number):String {
			var str:String = String(int(n));
			if (str.length < 2) {
				str = "0" + str;
			}
			return str;
		}
		
		private function output(str:*):void {
			msg.text = str;
		}

	}

}